package Controller;

import DataAccess.DataAccessFacade;
import DomainModel.Role;
import DomainModel.Section;
import DomainModel.User;
import java.io.IOException;
import java.util.List;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

/**
 *
 * @author Mershack, Reynier
 */
@WebServlet(name = "ViewSectionController", urlPatterns = {"/viewCourseSections"})
public class ViewSectionController extends HttpServlet {

    /**
     * Processes requests for both HTTP
     * <code>GET</code> and
     * <code>POST</code> methods.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        HttpSession session = request.getSession();
        User user = (User) session.getAttribute("currentUser");

        if (user == null) {
           request.getRequestDispatcher("/login").forward(request, response);
        }
    }

    // <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
    /**
     * Handles the HTTP
     * <code>GET</code> method.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
        
//        response.setContentType("text/html;charset=UTF-8");
//        PrintWriter out = response.getWriter();
        
        HttpSession session = request.getSession(); 
       
        try {                      
           //Get the sections and redirect to the adminViewSections or the viewSections page
            User u = (User)session.getAttribute("currentUser");
            List<Section> sections = null;

            if(u.getRole() == Role.ADMIN){//if admin get all courses                     
                sections = DataAccessFacade.getInstance().getAllSections();  
                request.setAttribute("sections", sections);
                request.getRequestDispatcher("/WEB-INF/grs/adminViewCourseSections.jsp").forward(request, response);
            }
            else { //get only the user specific courses 
                if (u.getRole() == Role.PROF) {
                    sections = DataAccessFacade.getInstance().getProfessorSections(u.getUserId());
                }
                else  if (u.getRole() == Role.STUD) {
                    sections = DataAccessFacade.getInstance().getStudentSections(u.getUserId());
                }
                
                request.setAttribute("sections", sections);
                request.getRequestDispatcher("/WEB-INF/grs/viewCourseSections.jsp").forward(request, response);
            }                

//            JSONArray sectionsJson = new JSONArray();
//
//            for (Section section : sections) {                    
//                JSONObject sectionJson = new JSONObject();     
//
//               /* sectionJson.put("courseID", course.getCourseId());
//                sectionJson.put("CourseName", course.getCourseName());
//                sectionJson.put("Section", course.getSection());
//                sectionJson.put("RoomNum", course.getRoomNum());                     */
//
//                sectionsJson.put(sectionJson);                    
//            }
//
//            String coursesStr = sectionsJson.toString();
//
//            out.print(coursesStr);   

       } catch (Exception e) {
       } finally {            
//        out.close();
       }
    }

    /**
     * Handles the HTTP
     * <code>POST</code> method.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
        
        String actionGotoSection = request.getParameter("gotoSection"); 
        if (actionGotoSection != null) {
            HttpSession session = request.getSession();
            User user = (User) session.getAttribute("currentUser");
            
            if (user.getRole() == Role.STUD) {
                String sectionId = request.getParameter("sectionId");
                response.sendRedirect("viewMyGrades?sid=" + sectionId);
            }
            else  if (user.getRole() == Role.PROF) {
                String sectionId = request.getParameter("sectionId");
                response.sendRedirect("viewClassWorks?sid=" + sectionId);
            }
        }
    }

    /**
     * Returns a short description of the servlet.
     *
     * @return a String containing servlet description
     */
    @Override
    public String getServletInfo() {
        return "Short description";
    }// </editor-fold>
}
